#include "MemoryPool.h"
#include "StackAlloc.h"
#include <cassert>
#include <ctime>
#include <iostream>
#include <vector>
const unsigned int ELEMS = 1e6;
const unsigned int REPEATS = 100;
int main()
{
    clock_t start;
    std::cout << "**********C++实现高性能内存池**********" << std::endl;
    // 使用内存池
    std::cout << "MemoryPool Allocator Time: ";
    StackAlloc<int, MemoryPool<int>> stackMemoryPool;
    start = clock();
    for (int j = 0; j < REPEATS; j++)
    {
        assert(stackMemoryPool.empty());
        for (unsigned int i = 0; i < ELEMS; i++)
        {
            stackMemoryPool.push(i);
        }

        for (unsigned int i = 0; i < ELEMS; i++)
            stackMemoryPool.pop();
    }
    std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "s" << std::endl;

    // 使用Vector
    std::cout << "Vector Allocator Time: ";
    std::vector<unsigned int> VectorDefault;
    start = clock();
    for (unsigned int j = 0; j < REPEATS; j++)
    {
        assert(VectorDefault.empty());
        for (unsigned int i = 0; i < ELEMS; i++)
            VectorDefault.push_back(i);
        for (unsigned int i = 0; i < ELEMS; i++)
            VectorDefault.pop_back();
    }
    std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "s" << std::endl;

    // 使用默认分配器
    std::cout << "Default Allocator Time: ";
    StackAlloc<unsigned int, std::allocator<unsigned int>> stackDefault;
    start = clock();
    for (unsigned int j = 0; j < REPEATS; j++)
    {
        assert(stackDefault.empty());
        for (unsigned int i = 0; i < ELEMS; i++)
            stackDefault.push(i);
        for (unsigned int i = 0; i < ELEMS; i++)
            stackDefault.pop();
    }
    std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "s" << std::endl;
    return 0;
}
